
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
	
// SVNKit ref
using org.tmatesoft.svn.core.wc;
using org.tmatesoft.svn.core;

namespace Subsync.Core.Subversion
{

	public class SVNRevert : CommandBase
	{
		public java.io.File[] filepath;

		public SVNRevert (string[] revertpaths)
		{
			Debug.WriteLine("SVN Revert constructor");
			int n = revertpaths.Length;
			java.io.File[] filepath = new java.io.File[n];
			for (int i=0;i<n;i++){
				filepath[i] = new java.io.File(revertpaths[i]);
			}
		}
		
		public override void Execute()
		{
			Debug.WriteLine("SVN Revert Execution");
					
			// Setup library
			Tools.setupLibrary();
			
			// Setup clients
			SVNClientManager cm = SVNClientManager.newInstance();
			SVNWCClient wcclient = cm.getWCClient();
			
			try {
				// Necessary to manually check?
				// Will the lib's method throw exptn of its own otherwise?
				// JimTODO: try
				
				/*foreach (java.io.File checkme in filepath){
					if (!checkme.exists())
						throw new SVNExecuteException("A path is invalid");
				}*/
				
				/* Erroneous input and exceptions
				 * Valid paths leading to invalid files seem to be ignored
				 * Invalid paths will throw exception
				 * How do we want to handle this */
					
				wcclient.doRevert(filepath, SVNDepth.INFINITY, null);
				
			} catch (SVNException expt) {
				throw new SVNExecuteException(expt.getMessage());
			} catch	(SVNExecuteException expt){
				throw expt;
			}
			
			
		}
		
		public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("revert",new List<object>() {"rv","revert"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers()
		{
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("revert",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens) 
		{
			if (tokens["revert"].Count==0){
				// Wise to write to console outside of UI class?
				Console.WriteLine("Revert requires at least one path");
				return;
			}
			this.setupRevert(tokens);
			this.Execute();
		}
		
		public SVNRevert() {}
		
		public void setupRevert(Dictionary<string,List<object>> tokens)
		{
			this.filepath = new java.io.File[tokens["revert"].Count];
			int n = tokens["revert"].Count;
			for (int i=0;i<n;i++){
				filepath[i] = new java.io.File((string)tokens["revert"][i]);
			}
		}
	}
}
